package com.le.basic.stringMatches;

import java.util.PriorityQueue;

public class Code_03_IPO {
    public static class Node {
        int cost;
        int profit;

        public Node(int cost, int profit) {
            this.cost = cost;
            this.profit = profit;
        }
    }

    public static int findMaximizedCapital(int k, int[] Profits, int[] Capital, int m) {
        Node[] nodes = new Node[Profits.length];
        for (int i = 0; i < Profits.length; i++) {
            nodes[i] = new Node(Capital[i], Profits[i]);
        }
        PriorityQueue<Node> costHeap = new PriorityQueue<>(((o1, o2) -> o1.cost - o2.cost));
        PriorityQueue<Node> profitHeap = new PriorityQueue<>(((o1, o2) -> o2.profit - o1.profit));
        for (int i = 0; i < nodes.length; i++) {
            costHeap.add(nodes[i]);
        }
        for (int i = 0; i < k; i++) {
            while (!costHeap.isEmpty() && costHeap.peek().cost <= k) {
                profitHeap.add(costHeap.poll());
            }
            if (profitHeap.isEmpty()) {
                return m;
            }
            m += profitHeap.poll().profit;
        }
        return m;
    }

    public static void main(String[] args) {
        int[] cost = {10, 30, 20, 70};
        int[] p = {2, 7, 10, 30};
        System.out.println(findMaximizedCapital(3, p, cost, 21));
    }
}
